package com.lkd.handler;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.lkd.business.MsgHandler;
import com.lkd.common.VMSystem;
import com.lkd.config.TopicConfig;
import com.lkd.contract.OrderCheck;
import com.lkd.emq.Topic;
import com.lkd.entity.OrderEntity;
import com.lkd.service.OrderService;
import com.lkd.utils.JsonUtil;
import org.elasticsearch.common.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;


@Component
@Topic(TopicConfig.ORDER_CHECK_TOPIC)
public class OrderCheckHandler implements MsgHandler {

    @Autowired
    private OrderService orderService;

    @Override
    public void process(String jsonMsg) throws IOException {
        //进行消息
        OrderCheck orderCheck = JsonUtil.getByJson(jsonMsg, OrderCheck.class);
        //如果参数无误
        if (orderCheck != null && !Strings.isNullOrEmpty(orderCheck.getOrderNo())){
            //查询该售货机的未支付的订单
            OrderEntity orderEntity = orderService.getOne(Wrappers.<OrderEntity>lambdaQuery()
                    .eq(OrderEntity::getOrderNo, orderCheck.getOrderNo())
                    .eq(OrderEntity::getStatus, VMSystem.ORDER_STATUS_CREATE));

            //如果有超时的 就修改订单
            if (orderEntity == null)return;

            //修改订单
            orderService.update(Wrappers.<OrderEntity>lambdaUpdate()
                    .eq(OrderEntity::getOrderNo, orderCheck.getOrderNo())
                    .set(OrderEntity::getStatus , VMSystem.ORDER_STATUS_INVALID));
        }



    }
}